﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using PetaPoco;
using Ninject;

namespace Meraz.Core.Repositories
{
    public interface IFacebookTopicPostRepository
    {
        FacebookTopicPost GetById(long boardID, int topicID, int postID);
        List<FacebookTopicPost> GetTopicPosts(long boardID, int topicID);
        void Add(FacebookTopicPost entity);
        void Delete(FacebookTopicPost entity);
        List<FacebookTopicPost> All();
        void Save(FacebookTopicPost entity);
        List<String> GetGenres();
        List<String> GetSubjects();
        int GetUniqueUserCount(long boardID, int topicID);
        int GetTotalPosts(long boardID, int topicID);
    }
    public class FacebookTopicPostRepository : IFacebookTopicPostRepository
    {
        [Inject]
        public Database DB { get; set; }
        public FacebookTopicPost GetById(long boardID, int topicID, int postID)
        {
            return DB.FirstOrDefault<FacebookTopicPost>("WHERE BoardID=@0 AND TopicID = @1 AND ID = @2", boardID, topicID, postID);
        }

        public List<FacebookTopicPost> GetTopicPosts(long boardID, int topicID)
        {
            return DB.Query<FacebookTopicPost>("WHERE BoardID=@0 AND TopicID = @1 order by createddateticks", boardID, topicID).ToList();
        }

        public void Add(FacebookTopicPost entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(FacebookTopicPost entity)
        {
            throw new NotImplementedException();
        }

        public List<FacebookTopicPost> All()
        {
            throw new NotImplementedException();
        }

        public void Save(FacebookTopicPost entity)
        {
            var p = DB.SingleOrDefault<FacebookTopicPost>("SELECT * FROM facebookpost WHERE BoardID=@0 AND TopicID = @1 AND ID = @2", entity.BoardID, entity.TopicID, entity.ID);
            if (p == null)
            {
                DB.Insert(entity);
            }
            else
            {
                DB.Update(entity);
            }
        }
        public List<String> GetGenres()
        {
            return DB.Query<String>("Select distinct genre from facebookpost order by genre").ToList();
        }
        public List<String> GetSubjects()
        {
            return DB.Query<String>("Select distinct subject from facebookpost order by subject").ToList();
        }
        public int GetUniqueUserCount(long boardID, int topicID)
        {
            return DB.ExecuteScalar<int>("Select distinct count(Poster) from facebookpost WHERE BoardID=@0 AND TopicID = @1", boardID, topicID);
        }
        public int GetTotalPosts(long boardID, int topicID)
        {

            return DB.ExecuteScalar<int>("Select count(*) from facebookpost WHERE BoardID=@0 AND TopicID = @1", boardID, topicID);
        }
    }


}
